/*
 * imagetest.java
 *
 * Created on 2008年10月12日, 上午10:39
 */

package isc;
import java.io.*;
import java.sql.*;
import java.awt.*;
import javax.swing.*;
/**
 *
 * @author  B
 */
public class imagetest extends javax.swing.JFrame {
    
    /** Creates new form imagetest */
    public imagetest() {
        initComponents();
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */


    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        test = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(121, 121, 121)
                        .addComponent(jButton1))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(106, 106, 106)
                        .addComponent(test, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(124, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addComponent(test, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(60, 60, 60)
                .addComponent(jButton1)
                .addContainerGap(75, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        
        this.insertimage("D:\\TP_PRUNED\\200910001001.jpg");
        this.readimage("D:\\TP_PRUNED\\xx.jpg","11");
    }//GEN-LAST:event_jButton1ActionPerformed
    
    public void insertimage(String path)
    {
         final String url="jdbc:mysql://localhost/isc?user=root&password=root&useUnicode=true";
         Connection imgcon=null;
         PreparedStatement imgpst=null;
         ResultSet rs=null;
         File file=null;
         FileInputStream inputimage=null;
         OutputStream outputimage=null;
         String id="13";

         try{
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("Driver loaded!");

            imgcon = DriverManager.getConnection(url);
            System.out.println("Database connected!");
            imgcon.setCatalog("isc");
            imgpst=imgcon.prepareCall("UPDATE test SET photo=? WHERE id=11");

            file=new File(path);
            try
            {
                inputimage=new FileInputStream(file);
            }
            catch (Exception e)
            {
            System.out.println(e);
            }

            byte[] buf=new byte[(int)file.length()];
            inputimage.read(buf);
            imgpst.setBytes(1, buf);

            imgpst.executeUpdate();
            System.out.println("commit successfully");

            inputimage.close();
            imgpst.close();
            imgcon.close();
         }
          catch(Exception e)
         {
            System.out.println(e);
         }

    }

    public void readimage(String path,String id){
        final String url="jdbc:mysql://localhost/isc?user=root&password=root&useUnicode=true";
        byte[] buffer=new byte[4096];
        Connection imgcon=null;
        PreparedStatement imgpst=null;
        ResultSet rs=null;
        File file=null;
        InputStream inputimage=null;
        OutputStream outputimage=null;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("Driver loaded!");

            imgcon = DriverManager.getConnection(url);
            System.out.println("Database connected!");
            imgcon.setCatalog("isc");
            imgpst=imgcon.prepareStatement("SELECT photo FROM test WHERE id=?");
            System.out.println("Select completed!");
            imgpst.setString(1,id);
            rs=imgpst.executeQuery();

            rs.next();
            file=new File(path);
            if(!file.exists())
            {
                try{
                 file.createNewFile();
                }
                catch(IOException e)
                {
                e.printStackTrace();
                }
            }
            try{
                outputimage=new FileOutputStream(file);
                System.out.println(outputimage.toString());
            }
            catch(FileNotFoundException e)
            {
                e.printStackTrace();
            }

                Blob image=rs.getBlob("photo");
                inputimage=image.getBinaryStream();
                int size;
                try{
                    while((size=inputimage.read(buffer))!=-1)
                    {
                     System.out.println(size);
                     outputimage.write(buffer, 0, size);
//                     outputimage.write(size);
                    }
                }
                    catch (IOException e) {
                    e.printStackTrace();
                    }

                outputimage.close();
                inputimage.close();
                imgpst.close();
                imgcon.close();
    }
        catch(Exception e)
        {
            System.out.println(e);
        }

        String img=path;
        Image imgTemp=Toolkit.getDefaultToolkit().createImage(img);
        test.setIcon(new ImageIcon(imgTemp));
    }
    
    
    
    /**
     * @param args the command line arguments
     */

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new imagetest().setVisible(true);

            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel test;
    // End of variables declaration//GEN-END:variables
    
}
